package de.tum.in.eist.im.client.Registration;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;

import de.tum.in.eist.im.client.Login.LoginPanel;
import de.tum.in.eist.im.shared.User.User;

public class RegistrationHandler implements ClickHandler, KeyUpHandler {

	private RegistrationPanel registrationPanel;
	
	// create a remote service proxy to talk to the server-side Login service.
	final private RegistrationServiceAsync registrationService = GWT.create(RegistrationService.class);
	
	/**
	 * constructor with a reference to the registration panel created an instance of this class
	 * @param registrationPanel
	 */
	public RegistrationHandler (RegistrationPanel registrationPanel) {
		this.registrationPanel = registrationPanel;
	}
	
	/**
	 * method called if the user clicks a key inside a widget referenced to this method
	 * @param event event which is sent
	 */
	@Override
	public void onKeyUp(KeyUpEvent event) {
		this.registration(this.registrationPanel.getUsername(), this.registrationPanel.getPassword(), this.registrationPanel.getRepeatedPassword());
	}

	/**
	 * method called if the user press a button referenced to this method
	 * @param event event which is sent
	 */
	@Override
	public void onClick(ClickEvent event) {
		this.registration(this.registrationPanel.getUsername(), this.registrationPanel.getPassword(), this.registrationPanel.getRepeatedPassword());
	}
	
	/**
	 * method which tries to register the user
	 */
	private void registration(String username, String password, String passwordRepeat) {
		
		this.registrationPanel.setErrorLabel("");
		
		if (username.isEmpty() || password.isEmpty() || passwordRepeat.isEmpty()) {
			this.registrationPanel.setErrorLabel("Pleas fill out all fields!");
		} else {	
			if (this.registrationPanel.getPassword().equals(this.registrationPanel.getRepeatedPassword())) {	
				/** TODO: 	Encrypt password before call! But beware! Localhost it seems works online not!
				 * 		    Error seems to be that SHAConverter is not really translated to js!
				 *			String encryptedPassword = null;
				 *			try {
				 *				encryptedPassword = SHAConverter.toSHA1(this.registrationPanel.getPassword());
				 *			} catch (Exception e){
				 *				registrationPanel.setErrorLabel("Couldn't encrypt password! Password: " + this.registrationPanel.getPassword());
				 *				return;
				 *			}
				 */
				User user = new User(username, password);
				registrationService.registerWithServer(user, 
					new AsyncCallback<Boolean>() {
						@Override
						public void onFailure(Throwable caught) {
							registrationPanel.setErrorLabel(caught.getMessage());
						}
	
						@Override
						public void onSuccess(Boolean result) {
							if (result) {
								LoginPanel loginPanel = new LoginPanel();
								loginPanel.setErrorLabel("Registration success!");
							} else {
								registrationPanel.setErrorLabel("Username already exists!");
							}
						}
				});
			} else {
				this.registrationPanel.setErrorLabel("The passwords don't match!");
			}
		}
	}
}
